package struct;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class InvServiceConfig implements ServiceConfig {
    private final static String GRP_ON_FIELD = "grp_on";
    private final static String INV_GEN_THRO_FIELD = "inv_gen_thro";
    private final static String CHK_FILES_FIELD = "chk_files";

    boolean grpOn;
    int invGenThro;
    List<String> chkFiles;

    public InvServiceConfig() {
    }

    public void setGrpOn(boolean grpOn) {
        this.grpOn = grpOn;
    }

    public void setInvGenThro(int invGenThro) {
        this.invGenThro = invGenThro;
    }

    public boolean getGrpOn() {
        return grpOn;
    }

    public int getInvGenThro() {
        return invGenThro;
    }

    public void setChkFiles(List<String> chkFiles) {
        this.chkFiles = chkFiles;
    }

    public List<String> getChkFiles() {
        return chkFiles;
    }

    @Override
    public String toString() {
        return toJSONObject().toJSONString();
    }

    public static InvServiceConfig fromJSONString(String json) {
        InvServiceConfig config = new InvServiceConfig();
        JSONObject jo = JSON.parseObject(json);
        config.grpOn = jo.getBooleanValue(GRP_ON_FIELD);
        config.invGenThro = jo.getIntValue(INV_GEN_THRO_FIELD);
        if (jo.containsKey(CHK_FILES_FIELD)) {
            JSONArray ja = jo.getJSONArray(CHK_FILES_FIELD);
            config.chkFiles = ja.toJavaList(String.class);
        }
        return config;
    }

    @Override
    public String toJSONString() {
        return toString();
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jo = new JSONObject();
        jo.put(GRP_ON_FIELD, grpOn);
        jo.put(INV_GEN_THRO_FIELD, invGenThro);
        if (chkFiles != null) {
            JSONArray ja = new JSONArray();
            ja.addAll(chkFiles);
            jo.put(CHK_FILES_FIELD, ja);
        }
        return jo;
    }
}
